﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FrontEndPlus.Módulos.Administración
{
    public partial class wbfrm_Carnet : System.Web.UI.Page
    {
        #region VARIABLES GLOBALES
        WCF_Gimnasio.ServiceClient _servicio = new WCF_Gimnasio.ServiceClient();
        WCF_Gimnasio.cls_Cliente _cliente = new WCF_Gimnasio.cls_Cliente();
        private String mensaje_error = String.Empty;
        #endregion

        #region METODOS

        private static byte[] ConvertHexToBytes(string hex)
        {
            byte[] bytes = new byte[hex.Length / 2];
            for (int i = 0; i < hex.Length; i += 2)
            {
                bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
            }
            return bytes;
        }

        [WebMethod(EnableSession = true)]
        public static string GetCapturedImage()
        {
            string url = HttpContext.Current.Session["CapturedImage"].ToString();
            HttpContext.Current.Session["CapturedImage"] = null;
            return url;
        }

        #endregion

        #region EVENTOS

        #region Cargar Página Page Load
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!this.IsPostBack)
                {
                    if (Request.InputStream.Length > 0)
                    {
                        using (StreamReader reader = new StreamReader(Request.InputStream))
                        {
                            string hexString = Server.UrlEncode(reader.ReadToEnd());
                            string imageName = txt_CodCliente.Text; //DateTime.Now.ToString("dd-MM-yy hh-mm-ss");
                            string imagePath = string.Format("~/Capturas/{0}.png", imageName);
                            File.WriteAllBytes(Server.MapPath(imagePath), ConvertHexToBytes(hexString));
                            Session["CapturedImage"] = ResolveUrl(imagePath);
                        }
                    }

                    if (Request.QueryString["idCliente"] != null)
                    {

                        txt_CodCliente.Text = Request.QueryString["idCliente"];
                        txt_Identificacion.Text = Request.QueryString["cedCliente"];
                        txt_Nombre.Text = Request.QueryString["nomCliente"];
                        txt_Apellido.Text = Request.QueryString["apeCliente"];

                        _cliente.FiltraConsulta = "SeleccionaPorId";
                        _cliente.IdUsuarioInicioSeccion = Convert.ToInt32(HttpContext.Current.Session["idUsuario"]);
                        _cliente.IdCliente = Convert.ToInt32(Request.QueryString["idCliente"]);
                        _cliente = (WCF_Gimnasio.cls_Cliente)_servicio.TraerObjeto(_cliente);

                        txt_Nombre_Carnet.Text = _cliente.Nombre + " " + _cliente.Apellido;
                        txt_Telefono_Carnet.Text = _cliente.Telefono.ToString();
                        txt_Cedula_Carnet.Text = _cliente.Cedula;

                    }
                }
            }
            catch (Exception ex)
            {
                string script = @"<script type='text/javascript'> smoke.alert('" + ex.Message + "', function (e) {}, {ok: 'OK',classname: 'custom-class'}); </script>";
                ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false);
            }
        }
        #endregion

        #region Evento Clic del Botón buscar Cliente
        protected void imgbtn_Busqueda_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                Response.Redirect("wbfrm_Busca_Cliente.aspx");
            }
            catch (Exception ex)
            {
                string script = @"<script type='text/javascript'> smoke.alert('" + ex.Message + "', function (e) {}, {ok: 'OK',classname: 'custom-class'}); </script>";
                ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false);
            }
        }
        #endregion

        protected void btn_Guardar_Click(object sender, EventArgs e)
        {
            if (Request.InputStream.Length > 0)
            {
                using (StreamReader reader = new StreamReader(Request.InputStream))
                {
                    string hexString = Server.UrlEncode(reader.ReadToEnd());
                    string imageName = txt_CodCliente.Text; //DateTime.Now.ToString("dd-MM-yy hh-mm-ss");
                    string imagePath = string.Format("~/Capturas/{0}.png", imageName);
                    File.WriteAllBytes(Server.MapPath(imagePath), ConvertHexToBytes(hexString));
                    Session["CapturedImage"] = ResolveUrl(imagePath);
                }
            }
        }

        #endregion

        protected void btnCapture_Click(object sender, EventArgs e)
        {

        }
    }
}